package org.desktop.web.server.listener;

import java.io.InputStreamReader;
import java.io.Reader;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.desktop.web.server.factory.ConfigurationFactory;

public class DesktopContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent ctx) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent ctx) {
		ConfigurationFactory fac = ConfigurationFactory.getInstance();
		try {
			String[] readers = StringUtils.split(ctx.getServletContext()
					.getInitParameter("properties"), ";");

			System.out.println("Loading properties..... ");
			for (int i = 0; i < readers.length; i++) {
				String r = readers[i];
				System.out.println("\tLoad "+r);
				Reader reader = new InputStreamReader(
						DesktopContextListener.class.getResourceAsStream(r));
				fac.load(reader);
				System.out.println("\t\t"+r+" loaded correctly");
			}
		} catch (ConfigurationException ex) {
			ex.printStackTrace();
		}
	}

}
